home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6584 / 6584.xpi / chrome / flashvideodownloader / content / download_frm_page.js < prev    next >
Text File  |  2009-11-26  |  13KB  |  415 lines

  1. var FlashVDFrm = {
  2.     
  3.     options : null,
  4.     
  5.     rootUrl : null,
  6.     urls : null,
  7.     nodeByUrl : null,
  8.     serviceNeed : null,
  9.     
  10.     elementsByUrl : {},
  11.  
  12.     init : function()
  13.     {
  14.         this.options = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("FlashVD.options.");
  15.         
  16.         this.rootUrl   = window.arguments[0];
  17.         this.urls      = window.arguments[1];
  18.         this.nodeByUrl = window.arguments[2];
  19.         this.serviceNeed = window.arguments[3];
  20.         
  21.         var folder = false;
  22.         try { folder = this.options.getComplexValue ("favorite-folder", Components.interfaces.nsISupportsString); } catch(e) {}
  23.         if(folder){window.document.getElementById('fav_folder_text').value=folder;}
  24.         
  25.         // add "Downliad All" button
  26.         if (this.urls.length > 1)
  27.         {
  28.             var bbox=document.createElement("box");
  29.             var bbutt=document.createElement("button");
  30.             var sitename=document.createElement("description");
  31.             var countInfo=document.createElement("description");
  32.  
  33.             bbutt.setAttribute("onclick","FlashVDFrm.download_all();");
  34.             bbutt.setAttribute("style","text-align:center;font-size:100%;font-weight:bold;");
  35.             bbutt.setAttribute("label","Download All ");
  36.             bbutt.setAttribute("image","chrome://flashvideodownloader/skin/16x16.png");
  37.             bbutt.setAttribute("dir","reverse");
  38.  
  39.             //add caption siteURL
  40.             sitename.appendChild(document.createTextNode(this.rootUrl + "..."));
  41.             sitename.setAttribute("style","padding-top:4px;");
  42.             
  43.             //add count info
  44.             countInfo.appendChild(document.createTextNode("(" + this.urls.length + " item(s))"));
  45.             countInfo.setAttribute("style","padding-top:4px;font-weight:bold;");
  46.             
  47.             //sobiraem vse vmeste i dobavlyaem v okno
  48.             bbox.appendChild(bbutt);
  49.             bbox.appendChild(sitename);
  50.             bbox.appendChild(countInfo);
  51.             bbox.setAttribute("style","padding:3px 0;");
  52.             document.getElementById('sites').appendChild(bbox);
  53.         }
  54.         
  55.         for(var i = 0; i < this.urls.length; i++)
  56.         {
  57.             var url=this.urls[i];
  58.  
  59.             var bbox=document.createElement("box");
  60.             var bbutt=document.createElement("button");
  61.             var sitename=document.createElement("description");
  62.             bbutt.setAttribute("onclick","FlashVDFrm.download_open(this);");
  63.             bbutt.setAttribute("style","text-align:center;font-size:100%;");
  64.             bbutt.setAttribute("onmouseover","FlashVDFrm.button_onmouseover(this);");
  65.             bbutt.setAttribute("onmouseout","FlashVDFrm.button_onmouseout(this);");
  66.             //bbutt.appendChild(document.createTextNode("download"));
  67.             bbutt.setAttribute("label","Download ");
  68.             bbutt.setAttribute("image","chrome://flashvideodownloader/skin/16x16.png");
  69.             bbutt.setAttribute("dir","reverse");
  70.             //delaem i dobavlyaem iconku
  71.             //var icon=document.createElement("image");
  72.             //icon.setAttribute("src","chrome://flashvideodownloader/skin/16x16.png");
  73.             //icon.setAttribute("style","padding:2px 0 0 4px;width:16px;height:16px;");
  74.             //bbutt.appendChild(icon);
  75.             //delaem nadpis siteURL
  76.             sitename.appendChild(document.createTextNode(url));
  77.             sitename.setAttribute("onmouseover","FlashVDFrm.label_onmouseover(this);");
  78.             sitename.setAttribute("onmouseout","FlashVDFrm.label_onmouseout(this);");
  79.             sitename.setAttribute("style","padding-top:4px;");
  80.             //sobiraem vse vmeste i dobavlyaem v okno
  81.             bbox.appendChild(bbutt);
  82.             bbox.appendChild(sitename);
  83.             bbox.setAttribute("style","padding:3px 0;");
  84.             document.getElementById('sites').appendChild(bbox);
  85.             
  86.             var elemsItem = { button : null, label : null };
  87.             elemsItem.button = bbutt;
  88.             elemsItem.label = sitename;
  89.             this.elementsByUrl[url] = elemsItem;
  90.         }
  91.     },
  92.     
  93.     getfilename : function (s)
  94.     {
  95.         var filename = s;
  96.         var p = filename.lastIndexOf("/");
  97.         if (p >= 0)
  98.         {
  99.             filename = filename.substring(p+1, filename.length);
  100.         }
  101.         return filename;
  102.     },
  103.  
  104.     button_onmouseover : function (elem)
  105.     {
  106.         try
  107.         {
  108.             var surl=elem.parentNode.lastChild.lastChild.data;
  109.             var node = this.nodeByUrl[surl];
  110.             this.onmouseover(node);
  111.         }
  112.         catch(e)
  113.         {
  114.             aler("over: " + e);
  115.         }
  116.     },
  117.  
  118.     button_onmouseout : function (elem)
  119.     {
  120.         try
  121.         {
  122.             var surl=elem.parentNode.lastChild.lastChild.data;
  123.             var node = this.nodeByUrl[surl];
  124.             this.onmouseout(node);
  125.         }
  126.         catch(e)
  127.         {
  128.             aler("out: " + e);
  129.         }
  130.     },
  131.  
  132.     label_onmouseover : function (elem)
  133.     {
  134.         try
  135.         {
  136.             var surl=elem.lastChild.data;
  137.             var node = this.nodeByUrl[surl];
  138.             this.onmouseover(node);
  139.         }
  140.         catch(e)
  141.         {
  142.             aler("over: " + e);
  143.         }
  144.     },
  145.  
  146.     label_onmouseout : function (elem)
  147.     {
  148.         try
  149.         {
  150.             var surl=elem.lastChild.data;
  151.             var node = this.nodeByUrl[surl];
  152.             this.onmouseout(node);
  153.         }
  154.         catch(e)
  155.         {
  156.             aler("out: " + e);
  157.         }
  158.     },
  159.  
  160.     onmouseover : function (node)
  161.     {
  162.         try
  163.         {
  164.             var oldBorder=node.style.border;
  165.             node.setAttribute("fvd-border",oldBorder);
  166.             node.style.border="3px dotted Chartreuse";
  167.             var oldDisplay=node.style.display;
  168.             node.setAttribute("fvd-display",oldDisplay);
  169.             node.style.display="block";
  170.         }
  171.         catch(e) {}
  172.     },
  173.  
  174.     onmouseout : function (node)
  175.     {
  176.         try
  177.         {
  178.             var oldBorder=node.getAttribute("fvd-border");
  179.             node.style.border=oldBorder;
  180.             var oldDisplay=node.getAttribute("fvd-display");
  181.             node.style.display=oldDisplay;
  182.         }
  183.         catch(e) {}
  184.     },
  185.     
  186.     download_open : function (elem)
  187.     {
  188.         var url=elem.parentNode.lastChild.lastChild.data;
  189.         this.download_url(url);
  190.     },
  191.     
  192.     download_all : function ()
  193.     {
  194.         for(var i = 0; i < this.urls.length; i++)
  195.         {
  196.             var url=this.urls[i];
  197.             
  198.             this.download_url(url);
  199.         }
  200.     },
  201.     
  202.     download_url : function (url)
  203.     {
  204.         if (this.serviceNeed[url] == true)
  205.         {
  206.             // replaced by opening window in new tab
  207.             //window.open('http://www.flashvideodownloader.org/download2.php?u='+url,"","width=500,height=500,resizable=no");
  208.             
  209.             // get the browser object
  210.             var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  211.                     .getService(Components.interfaces.nsIWindowMediator);
  212.             var mainWindow = wm.getMostRecentWindow("navigator:browser");
  213.             var browser = mainWindow.getBrowser();
  214.             
  215.             // add new tab with our url
  216.             var tab = browser.addTab('http://www.flashvideodownloader.org/download2.php?u='+url);
  217.             
  218.             // make the tab active
  219.             browser.selectedTab = tab;
  220.                         
  221.             return;
  222.         }
  223.         
  224.         try
  225.         {
  226.             var folder = this.get_folder_internal();
  227.             if (!folder)
  228.             {
  229.                 return;
  230.             }
  231.             
  232.             // try getDefaultFileName
  233.             var filename = this.getfilename(url);
  234.  
  235.             var uri = Components.classes["@mozilla.org/network/io-service;1"]
  236.                       .getService(Components.interfaces.nsIIOService)
  237.                       .newURI(url, null, null);
  238.            
  239.             //var hrefParts=/^(.*[^0-9])?([0-9]+)([^\/]*?\.[^\/]*?)$/.exec(url);
  240.             //var ext = /.*\.(.*?)$/.exec(hrefParts[3])[1];
  241.             //var filename=/^(.*?)(?:\.[^\.]{1,5})?$/.exec(filename)[1]+"."+ext;
  242.  
  243.             var file = Components.classes["@mozilla.org/file/local;1"]
  244.                         .createInstance(Components.interfaces.nsILocalFile);
  245.             file.initWithPath(folder + "\\" + filename); // download destination
  246.  
  247.             var fileUri = Components.classes["@mozilla.org/network/io-service;1"]
  248.                       .getService(Components.interfaces.nsIIOService)
  249.                       .newFileURI(file);
  250.  
  251.             // create a persist
  252.             var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
  253.                 createInstance(Components.interfaces.nsIWebBrowserPersist);
  254.  
  255.             // with persist flags if desired See nsIWebBrowserPersist page for more PERSIST_FLAGS.
  256.             const flags = persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
  257.             persist.persistFlags = flags | persist.PERSIST_FLAGS_FROM_CACHE;
  258.             
  259.  
  260.  
  261.             // transfer - these 3 lines of code shows standard FF Download Manager
  262.             //var transfer = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
  263.             //transfer.init(uri, fileUri, "", null, null, null, persist);
  264.             //persist.progressListener = transfer;
  265.             
  266.             // transfer - these 3 lines of code shows standard FF Download Manager
  267.             // + we want to know when donloading finished, so we create self progress listener and wrap the standard one by him
  268.             // owr progressListener call this.transferDone(url) when finished.
  269.             var transfer = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
  270.             transfer.init(uri, fileUri, "", null, null, null, persist);
  271.             var progress = new Progress(transfer, this, url);
  272.             persist.progressListener = progress;
  273.  
  274.             this.setDownloadingStyle(url, "start downloading");
  275.  
  276.             persist.saveURI(uri, null, null, null, "", file);
  277.         }
  278.         catch(e)
  279.         {
  280.             alert("There was an exception.\n\n'" + e + "\n\n" + url + "' download terminated.");
  281.         }
  282.     },
  283.  
  284.     setDownloadingStyle : function(url, state)
  285.     {
  286.         // 1. Find elements
  287.         //alert("setDownloadingStyle, url=" + url + ", state=" + state);
  288.         var elemsItem = this.elementsByUrl[url];
  289.         
  290.         // 2. Set elements style
  291.         // TODO: use somthing like enums instead simple strings
  292.         if (elemsItem == null)
  293.             return;
  294.  
  295.         if (state == "start downloading")
  296.         {
  297.             elemsItem.button.setAttribute("label","Downloading... ");
  298.             elemsItem.button.setAttribute("image","chrome://flashvideodownloader/skin/16x16.png");
  299.             elemsItem.label.setAttribute("style","padding-top:4px;color:gray;");
  300.         }
  301.         else if (state == "downloaded")
  302.         {
  303.             elemsItem.button.setAttribute("label","Downloaded ");
  304.             elemsItem.button.setAttribute("image","chrome://flashvideodownloader/skin/tick_16.png");
  305.             elemsItem.label.setAttribute("style","padding-top:4px;");
  306.         }
  307.         else //if (state == "any state")
  308.         {
  309.             elemsItem.button.setAttribute("label","Download ");
  310.             elemsItem.button.setAttribute("image","chrome://flashvideodownloader/skin/16x16.png");
  311.             elemsItem.label.setAttribute("style","padding-top:4px;");
  312.         }
  313.     },
  314.     
  315.     transferDone : function(status, request, url)
  316.     {
  317.         //alert("transferDone, url=" + url);
  318.         
  319.         var code=0;
  320.         try
  321.         {
  322.             var hc = request.QueryInterface(Components.interfaces.nsIHttpChannel);
  323.             code = hc.responseStatus;
  324.         } catch(e) {}
  325.  
  326.         if (status==0 && code==200)
  327.         {
  328.             this.setDownloadingStyle(url, "downloaded");
  329.         }
  330.         else
  331.         {
  332.             this.setDownloadingStyle(url, "err");
  333.         }
  334.     },
  335.     
  336.     get_folder_internal : function() {
  337.         var folder = null;
  338.         try { folder = this.options.getComplexValue ("favorite-folder", Components.interfaces.nsISupportsString); } catch(e) {}
  339.         if (folder == null || folder == "")
  340.             folder = this.get_folder();
  341.         return folder;
  342.     },
  343.     
  344.     get_folder : function() {
  345.         var folder = this.onPickFolder();
  346.         if (folder)
  347.             this.set_folder(folder);
  348.         return folder;
  349.     },
  350.  
  351.     set_folder : function(folder) {
  352.         var s = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  353.         s.data = folder;
  354.         this.options.setComplexValue("favorite-folder", Components.interfaces.nsISupportsString, s);
  355.         
  356.         window.document.getElementById('fav_folder_text').value=folder;
  357.     },
  358.     
  359.     onPickFolder : function() {
  360.         var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  361.         fp.init(window,'Choose folder for videos and games:', Components.interfaces.nsIFilePicker.modeGetFolder);
  362.         var res = fp.show();
  363.         if (res == Components.interfaces.nsIFilePicker.returnOK)
  364.             return fp.file.path;
  365.         return false;
  366.     }
  367. }
  368.  
  369.  
  370.  
  371.  
  372. function Progress(transfer,observer,url)
  373. {
  374.     this.transfer=transfer;
  375.     this.observer=observer;
  376.     this.url=url;
  377.     
  378.     //alert("Progress: url=" + url + ", this.url=" + this.url);
  379. }
  380.  
  381. Progress.prototype.onLocationChange=function(webProgress, request, location )
  382. {
  383.     this.transfer.onLocationChange(webProgress, request, location);
  384. }
  385.  
  386. Progress.prototype.onProgressChange=function(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress )
  387. {
  388.     try
  389.     {
  390.         this.transfer.onProgressChange(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress );
  391.     }
  392.     catch(e) {}
  393. }
  394.  
  395. Progress.prototype.onSecurityChange=function(webProgress, request, state )
  396. {
  397.     this.transfer.onSecurityChange(webProgress, request, state );
  398. }
  399.  
  400. Progress.prototype.onStateChange=function(webProgress, request, stateFlags, status )
  401. {
  402.     this.transfer.onStateChange(webProgress, request, stateFlags, status );
  403.     if(stateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
  404.     {
  405.         //alert("Progress.prototype.onStateChange: this.url=" + this.url);
  406.         this.observer.transferDone(status,request,this.url);
  407.     }
  408. }
  409.  
  410. Progress.prototype.onStatusChange=function(webProgress, request, status, message )
  411. {
  412.     this.transfer.onStatusChange(webProgress, request, status, message );
  413. }
  414.  
  415.